package com.jarvis.pzz.modules.my.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jarvis.pzz.R;
import com.jarvis.pzz.VcAdapter;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.BaseFragment;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.FeedBackModel;
import com.jarvis.pzz.models.ShopsListByFilterModel;
import com.jarvis.pzz.models.VCModel;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.my.adapter.FeedBackListAdapter;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.DividerItemDecoration;
import com.jarvis.pzz.util.net.RequestApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jarvis.liu on 2016-11-02.
 */

public class FeedbackListActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.rel_right)
    RelativeLayout rel_right;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_feed_back)
    TextView tv_feed_back;

    @BindView(R.id.recy_fd)
    ListView recy_fd;
    @BindView(R.id.ptr_fd)
    PtrClassicFrameLayout ptr_fd;
    private FeedBackListAdapter feedBackListAdapter;

    private int pageSize = 10;
    private int currentPage = 1;
    private int maxPage = 1;

    private RequestService service;
    private List<FeedBackModel> list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_feed_back_list;
    }

    @Override
    public void initData() {
        ptr_fd.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        ptr_fd.autoRefresh();
                    }
                }, 100);
    }

    @Override
    public void initView() {
        setStatusWhite(true);
        service = RequestApi.create(RequestService.class);
        setAdapter();
        setRefresh();
        setListener();
    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        rel_right.setOnClickListener(this);
        rel_left.setOnClickListener(this);
        tv_title.setOnClickListener(this);
        tv_feed_back.setOnClickListener(this);
    }

    /**
     * 设置下拉刷新
     */
    public void setRefresh() {
        ptr_fd.setMode(PtrFrameLayout.Mode.BOTH);
        ptr_fd.disableWhenHorizontalMove(true);
        ptr_fd.setLastUpdateTimeRelateObject(this);
        ptr_fd.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                return PtrDefaultHandler2.checkContentCanBePulledUp(frame, recy_fd, footer);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler2.checkContentCanBePulledDown(frame, recy_fd, header);
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                if (currentPage < maxPage) {
                    currentPage++;
                    loadData();
                } else {
                    ptr_fd.refreshComplete();
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                list.clear();
                currentPage = 1;
                loadData();
            }
        });
    }

    private void loadData() {
        Call<ResponseData<List<FeedBackModel>>> feedbackList = service.getFeedbackList();
        feedbackList.enqueue(new Callback<ResponseData<List<FeedBackModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<FeedBackModel>>> call, Response<ResponseData<List<FeedBackModel>>> response) {
                ptr_fd.refreshComplete();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        list.addAll(response.body().getData());
                        feedBackListAdapter.notifyDataSetChanged();
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData<List<FeedBackModel>>> call, Throwable t) {
                ptr_fd.refreshComplete();
                showTextToast(RequestApi.detailError(t));
            }
        });

    }

    /**
     * 设置adapter
     */
    public void setAdapter() {
        feedBackListAdapter = new FeedBackListAdapter(list, this);
        recy_fd.setAdapter(feedBackListAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        ptr_fd.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        ptr_fd.autoRefresh();
                    }
                }, 100);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
            case R.id.tv_feed_back:
                if(isLogin()){
                    startActivity(FeedbackActivity.class);
                }else {
                    startActivity(LoginActivity.class);
                }
                break;

        }
    }
}
